package com.example.zdxk.Decoder;

import com.example.zdxk.util.FormatUtil;

/**
 *        封装类，消息
 */
public class Message {
    private byte[] sync;                //同步头
    private byte[] msg_id;              //消息ID
    private byte mes_body_pro;          //消息体属性
    private byte[] imei;                //终端设备号imei
    private byte version_num;           //协议版本号
    private byte[] content;             //消息体
    private byte msg_body_termin;       //消息体结束符
    private byte check;                 //校验码
    private byte[] msg_termin;          //消息结束符

    /**
     *  将数据转换成消息报文   接收到消息后返回应答时使用
     * @return
     */
    public byte[] toData(){
        byte[] data;
        if(content != null) data=new byte[19+content.length];
        else data=new byte[19];

        data[0] = sync[0]; data[1] = sync[1]; data[2] = sync[2];                  //同步头
        data[3] = msg_id[0]; data[4] = msg_id[1];                                 //消息ID
        if(content != null)data[5] = (byte)content.length;                        //消息体属性  消息体属性
        else data[5] = 0;
        for(int i=0;i<imei.length;i++){                                           //iemi
            data[6+i]=imei[i];
        }
        data[14] = version_num;                                                    //版本号

        if(content != null) {
            for (int i=0;i<content.length;i++){                                        //消息体
                data[15+i]=content[i];
            }
        }

        data[data.length-4] = msg_body_termin;                                      //消息体结束符

        byte[] temp;
        if(content != null) {
            temp = new byte[16+content.length];
            System.arraycopy(data,0,temp,0,16+content.length);
        }else{
            temp = new byte[16];
            System.arraycopy(data,0,temp,0,16);
        }
        check = FormatUtil.getXor(temp);
        data[data.length-3] = check;                                                //校验码
        data[data.length-2] = msg_termin[0]; data[data.length-1] = msg_termin[1];   //消息结束符

        return data;
    }

    public byte[] toData( byte[] imei, byte[] id, byte[] content){
        byte[] data;
        if(null != content) data=new byte[19+content.length];
        else data=new byte[19];

        byte[] sync = {(byte)0x24,(byte)0x5A,(byte)0x44};						//同步头
        //byte prop = (byte)0x00;													//属性     .....
        byte version_num = (byte)0x01;											//版本号   .....
        byte mes_body_termi = (byte)0x23;										//消息体结束符
        byte check = (byte)0x00;												//校验码
        byte[] mes_termi = {(byte)0x0D,(byte)0x0A};								//消息结束符

        System.arraycopy(sync,0,data,0,3);              //同步头
        System.arraycopy(id,0,data,3,2);                //消息ID
        if(content != null)data[5] = (byte)content.length;                      //消息体属性  消息体属性
        else data[5] = 0;
        System.arraycopy(imei,0,data,6,8);              //IMEI
        data[14] = version_num;                                                //版本号     消息体之前

        if(null != content) System.arraycopy(content,0,data,15,content.length);       //消息体

        data[data.length-4] = mes_body_termi;                                       //消息体结束符
        System.arraycopy(mes_termi,0,data,data.length-2,2);     //消息结束符

        byte[] temp = new byte[data.length-3];
        System.arraycopy(data,0,temp,0,data.length-3);
        check = FormatUtil.getXor(temp);							//得出校验码
        data[data.length-3] = check;

        return data;
    }

    public byte[] getSync() { return sync; }

    public void setSync(byte[] sync) { this.sync = sync; }

    public byte[] getMsg_id() { return msg_id; }

    public void setMsg_id(byte[] msg_id) { this.msg_id = msg_id; }

    public byte getMes_body_pro() { return mes_body_pro; }

    public void setMes_body_pro(byte mes_body_pro) { this.mes_body_pro = mes_body_pro; }

    public byte[] getImei() { return imei; }

    public void setImei(byte[] imei) { this.imei = imei; }

    public byte getVersion_num() { return version_num; }

    public void setVersion_num(byte version_num) { this.version_num = version_num; }

    public byte[] getContent() { return content; }

    public void setContent(byte[] content) { this.content = content; }

    public byte getMsg_body_termin() { return msg_body_termin; }

    public void setMsg_body_termin(byte msg_body_termin) { this.msg_body_termin = msg_body_termin; }

    public byte getCheck() { return check; }

    public void setCheck(byte check) { this.check = check; }

    public byte[] getMsg_termin() { return msg_termin; }

    public void setMsg_termin(byte[] msg_termin) { this.msg_termin = msg_termin; }



}
